package cate.game.activity.optionalgift;

import cate.common.table.activity.optionalgift.row.OptionalGiftBaseRow;
import cate.common.table.d.GDActivity;
import cate.common.table.d.GDOperation;
import cate.common.table.pay.PayItemRow;
import cate.common.util.GameResult;
import cate.game.activity.base.core.RoleActivitySingle;
import cate.game.activity.optionalgift.msg.OptionalGiftInfoResp;
import cate.game.activity.optionalgift.po.OptionalGiftPo;
import cate.game.res.MixRes;
import cate.game.role.Role;
import easy.java.dev.note.NoteField;

import java.util.HashMap;
import java.util.Map;

public class OptionalGiftActivity extends RoleActivitySingle {
    @NoteField("不同挡位礼包")
    public Map<Integer, OptionalGiftPo> giftPoMap;

    @Override
    public void initialize(Role role) {
        super.initialize(role);
        if (giftPoMap == null) {
            giftPoMap = new HashMap<>();
        }
        giftPoMap.values().forEach(po -> po.initialize(this));
    }

    @Override
    public void onRoleOpen() {

    }

    @Override
    public void onRoleEnd() {
        giftPoMap.clear();
    }

    @Override
    public void initSend() {
        noticeUpdate();
    }

    @Override
    public void excelCheck() {

    }

    public GameResult<OptionalGiftActivity> chooseReward(int tid, Map<Integer, Integer> rewards) {
        GameResult<OptionalGiftActivity> r = new GameResult<>();
        r.data = this;
        OptionalGiftBaseRow baseRow = role.getGame().table.optionalGift.base.get(tid);
        if (baseRow == null) {
            return r.fail("找不到对应的礼包配置");
        }
        if (baseRow.configId != configTid()) {
            return r.fail("非本期礼包");
        }
        OptionalGiftPo po = giftPoMap.get(tid);
        if (po == null) {
            po = new OptionalGiftPo(tid, 0);
            po.initialize(this);
        }
        GameResult<Void> result = po.setReward(rewards);
        if (!result.ok()) {
            return r.fail(result.message);
        }
        giftPoMap.put(tid, po);
        return r;
    }

    @Override
    public GameResult<Void> allowPay(PayItemRow payItemRow) {
        GameResult<Void> r = new GameResult<>();
       int tid =  payItemRow.funcSubId;
        OptionalGiftBaseRow baseRow = role.getGame().table.optionalGift.base.get(tid);
        if (baseRow == null) {
            return r.fail("找不到对应的礼包配置");
        }
        if (baseRow.configId != configTid()) {
            return r.fail("非本期礼包");
        }
        OptionalGiftPo po = giftPoMap.get(tid);
        if (po == null) {
            return r.fail("礼包不存在");
        }
        if (po.getBaseRow().type != GDActivity.SHOP_TYPE_PAY) {
            return r.fail("不支持购买");
        }
        if (po.countItem() < po.getBaseRow().gridNum) {
            return r.fail("有未选的奖励");
        }
        if (po.getBaseRow().limit <= po.times) {
            return r.fail("礼包已买完");
        }

        return r;
    }

    @Override
    public void onPaySuccess(PayItemRow payItemRow) {
        //发放礼包
        int tid =  payItemRow.funcSubId;
        OptionalGiftPo po = giftPoMap.get(tid);
        if (po == null) {
            return;
        }
        MixRes reward = po.findRewards();
        if (reward == null || reward.isEmpty()) {
            return;
        }
        reward.addAndDialog(role, true, GDOperation.OPTIONAL_GIFT, tid);
        po.addTime();
//        po.clearReward();
        noticeUpdate();
    }

    //领取免费礼包
    public GameResult<OptionalGiftActivity> takeFreeReward(int tid) {
        GameResult<OptionalGiftActivity> r = new GameResult<>();
        r.data = this;
        OptionalGiftBaseRow baseRow = role.getGame().table.optionalGift.base.get(tid);
        if (baseRow == null) {
            return r.fail("找不到对应的配置");
        }
        if (baseRow.configId != configTid()) {
            return r.fail("非本期礼包");
        }
        OptionalGiftPo po = giftPoMap.get(tid);
        if (po == null) {
            return r.fail("礼包不存在");
        }
        if (po.getBaseRow().type != GDActivity.SHOP_TYPE_FREE) {
            return r.fail("不支持领取");
        }
        MixRes reward = po.findRewards();
        if (reward == null || reward.isEmpty()) {
            return r.fail("奖励为空");
        }
        if (po.getBaseRow().limit <= po.times) {
            return r.fail("礼包已买完");
        }
        reward.addAndDialog(role, true, GDOperation.OPTIONAL_GIFT_FREE, tid);
        po.addTime();
//        po.clearReward();
        return r;
    }

    public void noticeUpdate() {
        role.sendNow(new OptionalGiftInfoResp(this));
    }
}
